package com.say.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;

import com.say.bean.domain.Location;
import com.say.bean.domain.User;
import com.say.dao.LocationDao;
import com.say.framework.core.common.service.BaseServiceImpl;
import com.say.service.LocationService;
import com.say.service.UserService;


/**
 * 
 * 类名称：ConfigServiceImpl<br>
 * 类描述：系统参数服务接口实现<br>
 * @version v1.0
 *
 */
@Service("locationService")
public class LocationServiceImpl extends BaseServiceImpl<Location, String> implements LocationService {

    @Autowired
    public LocationDao locationDao;
    
    @Autowired
    public UserService userService;

    @Override
    public PagingAndSortingRepository<Location, String> getDao() {
        return locationDao;
    }
    
    private static final double EARTH_RADIUS = 6378137;
    private static double rad(double d)
    {
       return d * Math.PI / 180.0;
    }

	@Override
	public double getDistanceByUserId(String userId, String toUserId) {
		User user=userService.getById(userId);
		User toUser=userService.getById(toUserId);
		
		double radLat1 = rad(Double.parseDouble(user.getLatitude()));
        double radLat2 = rad(Double.parseDouble(toUser.getLatitude()));
        double a = radLat1 - radLat2;
        double b = rad(Double.parseDouble(user.getLongitude())) - rad(Double.parseDouble(toUser.getLongitude()));
        
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + 
        Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return new BigDecimal(s/1000).setScale(2, RoundingMode.HALF_UP).doubleValue();
	}
	
	

}
